package com.zzty.txj.controller;

import com.zzty.txj.entity.Daily;
import com.zzty.txj.entity.User;
import com.zzty.txj.service.DailyService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/daily")
public class DailyController {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DailyController.class);

    @Autowired
    DailyService dailyServiceImpl;

    @Autowired
    DailyService dailyService;

    @ResponseBody
    @RequestMapping(value="/insertTo", method=RequestMethod.POST)
    public Integer insertTo(Daily daily) {
        logger.info("daily" + daily.toString());

        Integer count=dailyServiceImpl.insertDaily(daily);
        logger.info("count" + count);

        return count;
    }

    @ResponseBody
    @RequestMapping(value="/getDaily")
    public List<Daily> getDaily() {
        List<Daily> dailies=new ArrayList<>();
        dailies=dailyServiceImpl.getDaily();
        return dailies;
    }

    @ResponseBody
    @RequestMapping(value="/deleteDaily", method=RequestMethod.POST)
    public Integer deleteDaily(Daily daily) {
        logger.info("daily" + daily.toString());

        Integer count=dailyServiceImpl.deleteDaily(daily);

        return count;
    }

    @RequestMapping("/getUserByOpenId")
    public User getDailyIdByUserId(@PathVariable("openId") String openId) {
        User user = new User();
        user = dailyService.getUserByOpenId(openId);
        logger.info("dailyController user: " + user);

        return user;
    }

    @RequestMapping("/getDailyIdByUserId")
    public String getDailyIdByUserId(@PathVariable("userId") Integer userId) {
        User user = new User();
        String dailyId = dailyService.getDailyIdByUserId(user.getId());
        return dailyId;
    }

    @RequestMapping("/getDailyById/{id}")
    public Map getDailyById(@PathVariable("id") Integer id) {
        Daily daily = dailyService.getDailyById(id);
        Map<Object, Object> map = new HashMap<>();
        String result;
        if (daily != null) {
            map.put("result", daily);

        } else {
            map.put("result", 0);
        }
        logger.info("DC getDailyById:" + map);

        return map;
    }

}
